package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.system.domain.bo.SysContractTypeAddBo;
import com.xjy.system.domain.bo.SysContractTypeEditBo;
import com.xjy.system.domain.bo.SysContractTypeQueryBo;
import com.xjy.system.domain.resp.SysContractTypeResp;
import com.xjy.system.domain.vo.SysContractTypeVo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.SysContractType;
import com.xjy.system.mapper.SysContractTypeMapper;
import com.xjy.system.service.ISysContractTypeService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 合同类型Service业务层处理
 *
 * @author xjy
 * @date 2022-02-08
 */
@Service
public class SysContractTypeServiceImpl extends ServicePlusImpl<SysContractTypeMapper, SysContractType> implements ISysContractTypeService {

    @Override
    public SysContractTypeVo queryById(Long id){
        return getVoById(id, SysContractTypeVo.class);
    }

    @Override
    public SysContractTypeResp queryPageList(SysContractTypeQueryBo bo) {
        PagePlus<SysContractType, SysContractTypeVo> result = pageVo(PageUtils.buildPagePlus()
			, buildQueryWrapper(bo), SysContractTypeVo.class);
		List<SysContractTypeVo> types = result.getRecordsVo();
		//根据大类去重
		ArrayList<SysContractTypeVo> maxTypes = types.stream().collect(Collectors.collectingAndThen(
			Collectors.toCollection(() ->
				new TreeSet<>(Comparator.comparing(SysContractTypeVo::getMaxType))), ArrayList::new));
		//根据小类去重
		ArrayList<SysContractTypeVo> minTypes = types.stream().collect(Collectors.collectingAndThen(
			Collectors.toCollection(() ->
				new TreeSet<>(Comparator.comparing(SysContractTypeVo::getMinType))), ArrayList::new));
		return SysContractTypeResp.builder().minTypes(minTypes).maxTypes(maxTypes).build();
    }

    @Override
    public List<SysContractTypeVo> queryList(SysContractTypeQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysContractTypeVo.class);
    }

    private LambdaQueryWrapper<SysContractType> buildQueryWrapper(SysContractTypeQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysContractType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMaxType() != null, SysContractType::getMaxType, bo.getMaxType())
        .eq(StrUtil.isNotBlank(bo.getMinType()), SysContractType::getMinType, bo.getMinType());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysContractTypeAddBo bo) {
        SysContractType add = BeanUtil.toBean(bo, SysContractType.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysContractTypeEditBo bo) {
        SysContractType update = BeanUtil.toBean(bo, SysContractType.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysContractType entity){
        //TODO 做一些数据校验,如唯一约束

    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public TableDataInfo<SysContractTypeVo> collects(SysContractTypeQueryBo bo) {
		PagePlus<SysContractType, SysContractTypeVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysContractTypeVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysContractType> queryPageListtype(String maxtype) {
		LambdaQueryWrapper lwq = new LambdaQueryWrapper<SysContractType>()
			.eq(SysContractType::getMaxType,maxtype);
    	return list(lwq);
	}
}
